<?php

require "html/extended/spClearBoth.php";
require "dialog/spDialog.php";

class spGuiListBoxFormBinding extends spDialog
{
	public function __construct( $param )
	{
		parent::__construct();
		parent::setClass( "spGuiListBoxFormBinding" );
	
		parent::addAttr( "data-formdatajson", json_encode($param) );
		
		list( $table, $left, $right ) = $this->createLayout();
		parent::addChild( $table );
	}
	
	///////////////////////////////////////////////////////////////////////////////////////////////////
	// 
	// createLayout: 
	//
	//	creates basics left and right layout for the listbox and forms
	//
	///////////////////////////////////////////////////////////////////////////////////////////////////
	
	public function createLayout()
	{
		$left = new spDiv(array(
			"class"	=> "containerLeft"
		));
		
		$right = new spDiv(array(
			"class"	=> "containerRight"
		));

		$table = new spTable2;
		$body = $table->addBody();
		$body->addCols(array(
			$left,
			$right
		));
		
		return array( $table, $left, $right );
	}

	///////////////////////////////////////////////////////////////////////////////////////////////////
	// 
	// doTrash: 
	//
	///////////////////////////////////////////////////////////////////////////////////////////////////

	static public function doTrash()
	{
		$id 		= $_REQUEST["id"];
		$dbClass 	= $_REQUEST["database"];

		$db = new $dbClass( $id );
		if ( !$db->trash() )
		{
			echo "ERROR: spGuiListBoxFormBinding::doTrash failed";
			var_dump( $_REQUEST );
			return;
		}
		
		echo "{}";
	}
	
	///////////////////////////////////////////////////////////////////////////////////////////////////
	// 
	// doDelete: 
	//
	///////////////////////////////////////////////////////////////////////////////////////////////////

	static public function doDelete()
	{
		$id = $_REQUEST["id"];
		$dbClass = $_REQUEST["database"];

		$db = new $dbClass( $id );
		if ( !$db->delete() )
		{
			echo '{"errors":["delete failed"]}';
			return;
		}
		
		echo "{}";		
	}
	
	///////////////////////////////////////////////////////////////////////////////////////////////////
	// 
	// createOneToMany: 
	//
	///////////////////////////////////////////////////////////////////////////////////////////////////

	// TODO: hide relationcol and unique id
	static public function createOneToMany()
	{
		//spDump( "createOneToMany", $_REQUEST );
		
		$dbClass 		= $_REQUEST["database"];
		$relationcol	= $_REQUEST["relationcol"];
		$parentid		= $_REQUEST["parentid"];

		$database = new $dbClass;
		$formBuilder = $database->createFormBuilder();
		
		$database->noDisplayPart( "id" );
		$database->noDisplayPart( $relationcol );
		$database->setValue( $relationcol, $parentid );

		$dlg = spDialog::create( array(
			"title" 		=> "New: ".$database->getName(),
			"bsavenew" 		=> "true",
			"data-database"	=> $dbClass
		));
		$dlg->addChild( $formBuilder );
		$dlg->render();			
	}
	
	///////////////////////////////////////////////////////////////////////////////////////////////////
	// 
	// create: 
	//
	//	handle create, makes a form for the database
	//
	///////////////////////////////////////////////////////////////////////////////////////////////////
	
	static public function create()
	{
		//spDump( "create", $_REQUEST );
		
		$dbClass 	= $_REQUEST["database"];

		$database = new $dbClass;
		$formBuilder = $database->createFormBuilder();

		$dlg = spDialog::create( array(
			"title" 		=> "New: ".$database->getName(),
			"bsavenew" 		=> "true",
			"data-database"	=> $dbClass
		));
		$dlg->addStyle( "width", "500px" );
		$dlg->addChild( $formBuilder );
		$dlg->render();			
	}

	static public function createSave()
	{
		$_REQUEST = unMagicQuotify($_REQUEST);

		$dbClass = $_REQUEST["database"];

		$database = new $dbClass;
		$database->updateFromRequest();
		if ( !$database->save() )
		{
			$errors = $database->getFormErrors();
			$json = array(
				"formerrors" => $errors
			);
			echo json_encode( $json );
		}
		else
		{
			echo '{"redirect":"noredirect"}';
		}	
	}

	///////////////////////////////////////////////////////////////////////////////////////////////////
	// 
	// editSave:
	//
	//	handle edit
	//
	///////////////////////////////////////////////////////////////////////////////////////////////////

	static public function showEditForm()
	{
//		spDump( "showEditForm", $_REQUEST );
		
		$dbClass 	= $_REQUEST["database"];
		$id 		= $_REQUEST["id"];

		$database = new $dbClass($id);
		$formBuilder = $database->createFormBuilder();

		$dlg = spDialog::create( array(
			"title" 		=> "Editing: ".$database->getIdentityTitle(),
			"bsave" 		=> "true",
			"data-id"		=> $id,
			"data-database"	=> $dbClass
		));
		$dlg->addStyle( "width", "500px" );
		$dlg->addChild( $formBuilder );
		$dlg->render();	
	}
	
	static public function editSave()
	{
		$_REQUEST = unMagicQuotify($_REQUEST);

		$db = $_REQUEST["database"];
		$id = $_REQUEST["id"];

		$database = new $db( $id );
		$database->updateFromRequest();
		if ( !$database->save() )
		{
			$errors = $database->getFormErrors();
			$json = array(
				"formerrors" => $errors
			);
			echo json_encode( $json );
		}
		else
		{
			echo '{"redirect":"noredirect"}';
		}	
	}

	///////////////////////////////////////////////////////////////////////////////////////////////////
	// 
	// updateListBox:
	//
	//	updates the listbox for given database
	//
	///////////////////////////////////////////////////////////////////////////////////////////////////
	
	static public function updateListBox()
	{
		$formdata = $_REQUEST["formdata"];
	
		//spDump( "updateListBox", $_REQUEST );

		/*
		parse_str( $_REQUEST["formdata"], $formdata );
		var_dump( $formdata );
		$dbClass = $formdata[0]["database"];
		*/
		
		$dbClass = $formdata["database"];

		$db = new $dbClass;
		$ar = $db->getGuiListBoxItems2();
		
		// we want the newest item on top
		$ar = array_reverse( $ar );
		
		$listbox = new spGuiListbox(array(
//			"width" 	=> "300px",
			"height"	=> "300px",
			"noborder"	=> "true",
			"min-width" => "300px"
		));
		
		// create items for listbox
		foreach ( $ar as $a )
		{
			$item = new spGuiListBoxItem(array(
				"content" => $a["content"]
			));
			
			if ( !isset($a["value"]) )
			{
				echo "ERROR: no value supplied for item in spGuiListBoxNED.php<br/>";
				echo "database: ".$dbClass."<br/>";
				spStackTrace();
				die();
			}
			
			$item->addClass( "updateInfo" );
			$item->addAttr(	"data-id", 		$a["value"] );
			$listbox->addItem( $item );	
		}
		
		$dlg = spDialog::create(array( 
			"title" => $db->getName()
		));
		
		$new = new spAnchor(array(
			"class" 	=> "formNewListBoxItem",
			"href" 		=> "",
			"content" 	=> "(new)",
			"attr"		=> array(
				"data-database" => $dbClass
			)
		));
		$dlg->addChild( $new );		
		$dlg->addChild( new spClearBoth );

		$dlg->addChild( $listbox );
		$dlg->render();
		
//		$listbox->render();
	}
	
	/*
	
		$_REQUEST required values:
		
			id
			database
	
	*/
	
	static public function oneToMany( $parentid, $onetomany )
	{
		$container = new spDiv;
	
		foreach ( $onetomany as $o )
		{
			$dbClass 		= $o["database"];
			$relationcol 	= $o["relationcol"];
			
			// find all $dbClass which have $relationcol=$parentid (this is our one-to-many relationship)
			$db = new $dbClass;
			$rows = $db->getRowsSql(array(
				"cols" => array(
					"id",
					$relationcol
				),
				"no_trashed" => "true"
			));
			
			// filter, only $relationcol=$parentid wanted
			$list = array();
			foreach ( $rows as $row )
			{
				if ( $row[$relationcol]==$parentid )
					$list[] = $row;
			}
			
			$c = spGuiListBoxFormBinding::createFieldSet( $db->getName() );
			$container->addChild( $c );
			
			$new = new spAnchor(array(
				"class" 	=> "formNewOneToMany",
				"href" 		=> "",
				"content" 	=> "(new)",
				"attr" 		=> array(
					"data-database" 	=> $dbClass,
					"data-relationcol" 	=> $relationcol,
					"data-parentid"		=> $parentid
				)
			));
			$c->addChild( $new );		
			$c->addChild( new spClearBoth );
		
			foreach ( $list as $form )
			{
				$formdatalayout = isset($o["formlayoutdata"]) ? $o["formlayoutdata"]:"";
						
				$id = $form["id"];
						
				// create database and formbuilder
				$database = new $db( $id );
				$fb = $database->getFormBuilder();
				$formBuilder = new $fb( $database );
				spGuiListBoxFormBinding::initializeFormBuilder( $formBuilder, $formdatalayout );
			
				$cc = spDialog::create(array(
					"title" 			=> $database->getIdentityTitle(),
					"roundedCorners" 	=> false, 
					"bedit" 			=> true, 
					"bdelete" 			=> true, 
					"btrash" 			=> true,
					"data-id"			=> $id,
					"data-database"		=> $dbClass,
					"data-name"			=> $database->getIdentityTitle()
				));
				$c->addChild( $cc );
				$c->addChild( new spBreak );
			
				$cc->addChild( $formBuilder );
			}
		}
		
		return $container;
	}
	
	static public function showForm()
	{
//		spDump( "showForm", $_REQUEST );
//		parse_str( $_REQUEST["formdata"], $formdata );
//		spDump( "formdata", $formdata );
	
		$formdata = $_REQUEST["formdata"];
	
		$id 		= $_REQUEST["id"];
		$db 		= $formdata["database"];

		// create database and formbuilder
		$database = new $db( $id );
		$fb = $database->getFormBuilder();
		$formBuilder = new $fb( $database );
		spGuiListBoxFormBinding::initializeFormBuilder( $formBuilder );
		
		$title = $database->getIdentityTitle();
		
		$container = spDialog::create(array( 
			"title" 			=> $title, 
			"roundedCorners" 	=> true, 
			"bedit" 			=> true, 
			"bdelete" 			=> true, 
			"btrash" 			=> true,
			"data-id"			=> $id,
			"data-database"		=> $db,
			"data-name"			=> $title
		));
		$container->addStyle( "width", "500px" );
		$container->addChild( $formBuilder );

		if ( isset($formdata["onetomany"]) )
		{
			$c = spGuiListBoxFormBinding::oneToMany( $id, $formdata["onetomany"] );
			$container->addChild( $c );
		}
			
		$container->render();
	}
	
	static public function initializeFormBuilder( $formBuilder, $formdatalayout="" )
	{
		$formBuilder->showSubTitles( false );
		$formBuilder->showRequired( false );
		$formBuilder->setReadOnly( true );

		if ( $formdatalayout!="" )
			$formBuilder->setFormLayoutDefinition( $formdatalayout );
	}
	
}

?>